package com.mogujie.calldemo

import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.telephony.PhoneStateListener
import android.telephony.TelephonyManager
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView


class PhoneReceiver : BroadcastReceiver() {
    private var mcontext: Context? = null
    private var wm: WindowManager? = null
    override fun onReceive(context: Context, intent: Intent) {
        mcontext = context
        println("action" + intent.action)
        if (intent.action == Intent.ACTION_NEW_OUTGOING_CALL) {
            //如果是去电（拨出）
            Log.e("TAG", "拨出")
        } else {
            //查了下android文档，貌似没有专门用于接收来电的action,所以，非去电即来电
            Log.e("TAG", "来电")
            val tm = context.getSystemService(Service.TELEPHONY_SERVICE) as TelephonyManager
            tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE)
            //设置一个监听器
        }
    }

    private val tv: TextView? = null
    private var inflate: LayoutInflater? = null
    private var phoneView: View? = null
    private val listener: PhoneStateListener = object : PhoneStateListener() {
        override fun onCallStateChanged(state: Int, incomingNumber: String) {
            // TODO Auto-generated method stub
            //state 当前状态 incomingNumber,貌似没有去电的API
            super.onCallStateChanged(state, incomingNumber)
            when (state) {
                TelephonyManager.CALL_STATE_IDLE -> {
                    Log.e("TAG", "挂断")
                    wm?.removeView(phoneView)
                }
                TelephonyManager.CALL_STATE_OFFHOOK -> {
                    Log.e("TAG", "接听")
                    wm?.removeView(phoneView)
                }
                TelephonyManager.CALL_STATE_RINGING -> {
                    inflate = LayoutInflater.from(mcontext)
                    wm =
                        mcontext!!.applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
                    val params = WindowManager.LayoutParams()
                    params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

                    params.flags =
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    params.gravity = Gravity.CENTER
                    params.width = WindowManager.LayoutParams.MATCH_PARENT
                    params.height = 600
                    params.format = PixelFormat.RGBA_8888
                    phoneView = inflate?.inflate(R.layout.phone_text, null)
                    val text = phoneView?.findViewById<TextView>(R.id.text)
                    text?.setText(incomingNumber)
                    wm!!.addView(phoneView, params)
                    Log.e("TAG", "响铃:来电号码$incomingNumber")
                    Log.e("TAG", "响铃:======" + Thread.currentThread().name)
                }
            }
        }
    }
}